package com.dss.auto_deploy.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dss.auto_deploy.domain.AdmNacos;
import com.dss.auto_deploy.dto.AdmNacosAddDto;
import com.dss.auto_deploy.dto.AdmNacosPageDto;
import com.dss.auto_deploy.dto.AdmNacosUpdateDto;
import com.dss.auto_deploy.service.IAdmNacosService;
import com.dss.auto_deploy.util.Result;
import com.dss.auto_deploy.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
* nacos配置
* @Description:nacos配置控制层
* @author dongshoushan
* @date 2022-10-22 21:48:18
*/
@RestController
@RequestMapping("/admNacos")
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
@Api(tags ="99: nacos配置")
public class AdmNacosController {

    /**
    * nacos配置Service
    */
    private final  IAdmNacosService admNacosService;
    private final HttpServletRequest request;
    /**
    * 新增nacos配置
    * @param addDto
    * @return
    */
    @PostMapping
    @ApiOperation(value = "新增nacos配置", notes="新增nacos配置")
    public Result insert(@RequestBody @Valid AdmNacosAddDto addDto) {
        admNacosService.insert(addDto);
        return Result.success("新增成功");
    }

    /**
    * 修改nacos配置
    * @param updateDto
    * @return
    */
    @PutMapping
    @ApiOperation(value = "修改nacos配置", notes="修改nacos配置")
    public Result update(@RequestBody @Valid AdmNacosUpdateDto updateDto) {
        admNacosService.update(updateDto);
        return Result.success("修改成功");
    }

    /**
    * 批量删除nacos配置
    * @param ids
    * @return
    */
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除nacos配置", notes="删除nacos配置")
    public Result deleteBatch(@PathVariable Long[] ids) {
        admNacosService.deleteBatch(ids);
        return Result.success("删除成功");
    }

    /**
    * 分页查询nacos配置
    * @param pageDto
    * @return
    */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询nacos配置", notes="分页查询nacos配置")
    public Result page(AdmNacosPageDto pageDto) throws InterruptedException {
        return Result.success(admNacosService.list(pageDto));
    }
    @GetMapping("/getAll")
    @ApiOperation(value = "查询所有nacos配置", notes="查询所有nacos配置")
    public List<AdmNacos> getAll(String name){
        return admNacosService.list(Wrappers.lambdaQuery(AdmNacos.class).like(StringUtils.isNotEmpty(name),AdmNacos::getNacosName, name).orderByAsc(AdmNacos::getNacosName));
    }

    @ApiOperation(value = "nacos安装", notes="nacos安装")
    @PostMapping(value = "install", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result installnacos(
            @RequestParam(value = "ids") String ids,
            @RequestParam(value = "tempPath") String tempPath,
            @RequestPart(value = "nacosFile") MultipartFile nacosFile,
            @RequestPart(value = "plugFile",required = false) MultipartFile plugFile) throws Exception {
        request.getInputStream().skip(request.getContentLength());
        admNacosService.installnacos(ids,tempPath,nacosFile,plugFile);
        return Result.success();
    }

}
